package ce.com.newbluedemo.my;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * Created by Administrator on 2016/11/22 0022.
 */
public class testJava {
    public static void main(String [] s){

        int a = 0xf1&0xff;
//        int a=0x50;
        int tCurrentIndex = (a & 0x0f);

        int tTotalIndex = (a >> 4);

        System.out.println("tCurrentIndex: "+tCurrentIndex + " tTotalIndex: " + tTotalIndex);



        String aaa="merchId=23&nonceStr="+genRandomNum()+"&phone=13590269200&verifyCode=018256&key=E18ADC3949BA59ABBE56E057F20F883E";

        String s1=   getMD5(aaa).toUpperCase();
        String s2=   encode(aaa).toUpperCase();

        System.out.println("s1: "+s1 );
        System.out.println("s2: "+s2 );

        System.out.println("s3:"+genRandomNum());

        //DCB0B4D0F76E77ED88C23491491DE2AB
    }
    public static String genRandomNum(){
        int  maxNum = 36;
        int i;
        int count = 0;
        char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while(count < 16){
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count ++;
            }
        }
        return pwd.toString();




    }




    public static String getMD5(String info)
    {
        try
        {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(info.getBytes("UTF-8"));
            byte[] encryption = md5.digest();

            StringBuffer strBuf = new StringBuffer();
            for (int i = 0; i < encryption.length; i++)
            {
                if (Integer.toHexString(0xff & encryption[i]).length() == 1)
                {
                    strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
                }
                else
                {
                    strBuf.append(Integer.toHexString(0xff & encryption[i]));
                }
            }

            return strBuf.toString();
        }
        catch (NoSuchAlgorithmException e)
        {
            return "";
        }
        catch (UnsupportedEncodingException e)
        {
            return "";
        }
    }


        // 进行md5的加密运算
        public static String encode(String password) {
            // MessageDigest专门用于加密的类
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                byte[] result = messageDigest.digest(password.getBytes()); // 得到加密后的字符组数

                StringBuffer sb = new StringBuffer();

                for (byte b : result) {
                    int num = b & 0xff; // 这里的是为了将原本是byte型的数向上提升为int型，从而使得原本的负数转为了正数
                    String hex = Integer.toHexString(num); //这里将int型的数直接转换成16进制表示
                    //16进制可能是为1的长度，这种情况下，需要在前面补0，
                    if (hex.length() == 1) {
                        sb.append(0);
                    }
                    sb.append(hex);
                }

                return sb.toString();

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return null;
            }
        }

}
